﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using SYT.AliApiHandle;
using Data.Service;

namespace SYT.Web.Controllers
{
    public class HomeController : Controller
    {
        //主页
        public ActionResult Index()
        {
            string LoginObj = System.Configuration.ConfigurationManager.AppSettings["LoginObj"].ToString();
            Model.Users usere = UsersData.GetUser(LoginObj);

            if (usere == null || (DateTime.Now - usere.CreateTime).TotalMinutes > 600)
            {
                //获取Code;
                if (Session["UserreAllInfo"] == null || Session["UserreAllInfo"].ToString() == "")
                {
                    string urlstr = GetToken.GetCodeSkipMyUrl();
                    Response.Redirect(urlstr);
                    return null;
                }
                else
                {
                    Users user = JsonConvert.DeserializeObject<Users>(Session["UserreAllInfo"].ToString());
                    ViewBag.username = user.resource_owner;
                    //return View();

                    return RedirectToAction("Index1", "Product");
                }
            }
            else
            {
                 Session["UserreAllInfo"] =  JsonConvert.SerializeObject(usere);

                 return RedirectToAction("Index1", "Product");
            }
        }

        //接收阿里返回的Code.
        public ActionResult AlRequstCode()
        {
            string code = Request["code"];

            //调用获取令牌方法
            bool isNOone = false;
            //存入Session的UserreAllInfo用户所有信息。
            Session["UserreAllInfo"] = GetToken.getTokenFunc(code, out isNOone);
            if (isNOone)
            {
                
                return RedirectToAction("Index1", "Product");
            }
            else
            {
                
                return RedirectToAction("Index1", "Product");
            }
        }
    }
}
